package com.quasar.hpatient.module.comm_login;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.module.comm_compile.CompileActivity;
import com.quasar.hpatient.module.comm_regist.RegistActivity;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.util.InputUtil;
import lib.quasar.widget.warning.WarningView;

/**
 * description: 登录
 * created by kalu on 2018/4/2 15:31
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginView {

    public final static int REQUEST_REGIST = 1004;

    @Override
    public int initView() {
        return R.layout.activity_comm_login;
    }

    @Override
    public void initDataLocal() {

        final TextView login = findViewById(R.id.activity_login_push);
        if (null != login) {
            login.setOnClickListener(v -> {
                LoginPresenter presenter = getPresenter();
                if (null == presenter) return;
                final EditText account = findViewById(R.id.activity_login_userId);
                final EditText password = findViewById(R.id.activity_login_password);
                presenter.loginAccount(LoginActivity.this, account, password);
            });
        }

        final TextView regist = findViewById(R.id.activity_login_regist);
        if (null != regist) {
            regist.setOnClickListener(v -> {
                Intent intent = new Intent(getApplicationContext(), RegistActivity.class);
                intent.putExtra(RegistActivity.NAME, RegistActivity.NAME_REGIST);
                startActivityForResult(intent, REQUEST_REGIST);
            });
        }

        final TextView forget = findViewById(R.id.activity_login_forget);
        if (null != forget) {
            forget.setOnClickListener(v -> {
                final Intent intent = new Intent(getApplicationContext(), RegistActivity.class);
                intent.putExtra(RegistActivity.NAME, RegistActivity.NAME_FORGET);
                goNext(intent);
            });
        }

        final ImageView eye = findViewById(R.id.activity_login_eye);
        if (null != eye) {
            eye.setOnClickListener(v ->  changeInputType(R.id.activity_login_password, R.id.activity_login_eye));
        }
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void goMain(Intent intent) {
        goNext(intent);
        onBackPressed();
    }

    @Override
    public void changeInputType(int editId, int imageId) {
        EditText input = findViewById(editId);
        if (null == input || input.getText().length() == 0) return;

        ImageView image = findViewById(imageId);
        if (null == image) return;

        boolean ok = (input.getTransformationMethod() == PasswordTransformationMethod.getInstance());
        image.setImageDrawable(getResources().getDrawable(ok ? R.drawable.ic_comm_regist_password_on : R.drawable.ic_comm_regist_password_off));
        input.setTransformationMethod(ok ? HideReturnsTransformationMethod.getInstance() : PasswordTransformationMethod.getInstance());
        input.setSelection(input.getText().length());
    }

    @Override
    public void warning(String text) {
        final WarningView warning = findViewById(R.id.activity_login_msg);
        if (null == warning) return;
        warning.setWarning(text);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode != REQUEST_REGIST || resultCode != RegistActivity.RESULT_COMPILE) return;
        toast("注册成功, 请完善信息");
        goNext(CompileActivity.class);
        onBackPressed();
    }
}